package com.qf.controller;

import com.qf.Excaption.MyExcaption;
import com.qf.domain.NewsComment;
import com.qf.service.NewsCommentService;
import com.qf.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@ResponseBody
@RequestMapping("/comment")
public class NewsCommentController {
    @Autowired
    private NewsCommentService newsCommentService;
    @GetMapping("/list/{newid}")
    public ResultVO selectcomment(@PathVariable(value = "newid") String newid){
        System.out.println(newid);
        if (newid!=""&&newid!=null) {
            ResultVO resultVO = newsCommentService.selectcomment(Integer.parseInt(newid));
            return resultVO;
        }else {
            throw  new MyExcaption("没有接收道数据",500);
        }
    }
    @PostMapping("/commentadd")
    public ResultVO insertadd(@Valid NewsComment newsComment, BindingResult bindingResult){
        ResultVO resultVO=new ResultVO();
        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();
            resultVO.setMsg(message);
            resultVO.setCode(400);
            return resultVO;
        }else {
            newsComment.setCreatedate(new Date());
            int i=newsCommentService.insertadd(newsComment);
            if (i!=1){
                resultVO.setMsg("添加失败");
                resultVO.setCode(400);
                return resultVO;
            }else {
                return resultVO;
            }

        }

    }
}
